package org.urchr.beorganized.api.impl.permission;

import java.util.List;

import org.urchr.beorganized.api.base.PrivateBaseAPIServlet;
import org.urchr.beorganized.dao.NotesDAO;
import org.urchr.beorganized.dao.PermissionsDAO;
import org.urchr.beorganized.dao.XMLDocumentBuilder;
import org.urchr.beorganized.dao.errors.GeneralException;
import org.urchr.beorganized.dao.tables.Permission;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class PermissionList extends PrivateBaseAPIServlet {

	@Override
	protected void doProcessPrivate() throws GeneralException {
		int noteID = NotesDAO.safeInt(request.getParameter("noteid"), -1);
		List<Permission> list = PermissionsDAO.getPermissions(userID, noteID);
		XMLDocumentBuilder doc = new XMLDocumentBuilder();
		doc.getRootElement("list");
		for (Permission permission : list) {
			Element e = doc.createElement("perm");
			e.setAttribute("id", ""+permission.getId());
			e.setAttribute("login", permission.getUser().getLogin());
			e.setAttribute("isCreate", ""+(permission.getAllowCreate()>0));
			e.setAttribute("isEdit", ""+(permission.getAllowEdit()>0));
			e.setAttribute("isDelete", ""+(permission.getAllowDelete()>0));
			doc.getRootElement().appendChild(e);
		}
		sendNode(doc.getRootElement());
	}

}
